package com.xx.bysj.config;
import com.xx.bysj.security.MyAuthenticationFailureHandler;
import com.xx.bysj.security.MyAuthenticationSuccessHandler;
import com.xx.bysj.security.MyUserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailServiceImpl myUserDetailServiceImpl;

    @Autowired
    private MyAuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private MyAuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    public SpringSecurityConfig(MyUserDetailServiceImpl myUserDetailService) {
        this.myUserDetailServiceImpl = myUserDetailService;
    }

    /**
     * 密码加密使用SpringSecurity自带的加密算法
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 密码认证
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailServiceImpl).passwordEncoder(passwordEncoder());
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * @param http 自定义认证方法
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 自定义认证方法
        // 开启认证
        http.authorizeRequests()
                // mvcMatchers地址,permitAll全部通过
                /*.mvcMatchers("/admin").permitAll()*/
                .mvcMatchers("/user/register").permitAll() // 放行注册请求
                .mvcMatchers("/securityRegister").permitAll()  // 放行注册页面
                .mvcMatchers("/securityLogin").permitAll()     // 放行登录页面
                .mvcMatchers("/email/sendEmail").permitAll()     // 放行邮箱发送页面
                // anyRequest任何请求,authenticated需要认证
                .anyRequest().authenticated()
                // formLogin通过表单形式
                .and()
                .formLogin()
                // 自定义登录页面
                .loginPage("/securityLogin")
                .loginProcessingUrl("/login")
                /*.successForwardUrl("/index")*/
                /*.defaultSuccessUrl("/index",true)
                .failureUrl("/securityLogin")*/
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .and()
                .rememberMe()
                .and()
                .csrf().disable()// 禁止 crsf
                .headers().frameOptions().disable();// springSecurity需要添加这个否则会导致layui中无法跳转页
    }

    /**
     * @param web 放行静态资源
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**");
        web.ignoring().antMatchers("/api/**");
        web.ignoring().antMatchers("/images/**");
        web.ignoring().antMatchers("/js/**");
        web.ignoring().antMatchers("/lib/**");

    }
}
